<template>
  <section>
    <!--工具条-->
    <toolbar :buttonList="buttonList" @callFunction="callFunction"></toolbar>
    <el-pagination
      layout="prev, pager, next"
      @current-change="handleCurrentChange"
      :page-size="page.PageSize"
      :total="page.PageCount"
      style="float: right"
    >
    </el-pagination>
    <!--列表-->
    <el-table
      :data="datalist"
      highlight-current-row
      v-loading="listLoading"
      @row-click="selsChange"
      style="width: 100%"
      ref="mytable"
    >
      <el-table-column type="selection" width="50"> </el-table-column>
      <el-table-column width="80">
        <template slot-scope="scope">
          {{ (page.PageIndex - 1) * page.PageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="Path" label="接口地址" width="" sortable>
      </el-table-column>
      <el-table-column prop="Name" label="功能简介" width="300" sortable>
      </el-table-column>
      <el-table-column
        prop="InsertTime"
        label="创建时间"
        :formatter="formatCreateTime"
        width=""
        sortable
      >
      </el-table-column>
      <el-table-column prop="Enabled" label="状态" width="" sortable>
        <template slot-scope="scope">
          <el-tag
            :type="!scope.row.Enabled ? 'success' : 'danger'"
            disable-transitions
            >{{ scope.row.Enabled ? "禁用" : "正常" }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :page-size="page.PageSize"
        :total="page.PageCount"
        style="float: right"
      >
      </el-pagination>
    </el-col>
    <!--新增界面-->
    <el-dialog
      title="新增"
      :visible.sync="addFormVisible"
      v-model="addFormVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="addForm"
        label-width="80px"
        :rules="addFormRules"
        ref="addFormRef"
      >
        <el-form-item label="接口地址" prop="Path">
          <el-input
            v-model="addForm.Path"
            maxlength="500"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="接口描述" prop="Name">
          <el-input
            v-model="addForm.Name"
            maxlength="500"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="启用状态" prop="Enabled">
          <el-select v-model="addForm.Enabled" placeholder="请选择状态">
            <el-option label="禁用" :value="true"></el-option>
            <el-option label="启用" :value="false"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click.native="saveSubmit"
          :loading="addLoading"
          >提交</el-button
        >
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from "../../../util/date";
import { getApiListPage, removeApi, saveApi } from "../../api/api";
import { getButtonList } from "../../promissionRouter";
import Toolbar from "../../components/Toolbar";
import qs from "qs";
export default {
  components: { Toolbar },
  data() {
    return {
      buttonList: [],
      currentRow: null,
      filters: {
        name: "",
      },
      datalist: [],
      page: {
        PageSize: 10,
        PageIndex: 1,
        PageCount: 0,
        Where: "",
      },
      listLoading: false,
      select: {}, //列表选中列
      addDialogFormVisible: false,
      addFormVisible: false, //新增界面是否显示
      addLoading: false,
      addFormRules: {
        Path: [{ required: true, message: "请输入接口地址", trigger: "blur" }],
        Name: [{ required: true, message: "请输入功能简介", trigger: "blur" }],
      },
      //新增界面数据
      addForm: {
        Path: "",
        Name: "",
        Enabled: false,
      },
    };
  },
  methods: {
    callFunction(item) {
      this.filters = {
        name: item.search,
      };
       if(item.Func=='search')
      {
        this.handleCurrentChange(1);
      }
      this[item.Func].apply(this, item);
    },
    //性别显示转换
    formatEnabled: function (row, column) {
      return row.Enabled ? "正常" : "未知";
    },
    formatCreateTime: function (row, column) {
      return !row.InsertTime || row.InsertTime == ""
        ? ""
        : util.formatDate.format(new Date(row.InsertTime), "yyyy-MM-dd");
    },
    handleCurrentChange(val) {
      this.page.PageIndex = val;
      this.search();
    },
    //获取用户列表
    search() {
      this.page.Where = this.filters.name;
      this.listLoading = true;
      getApiListPage(this.page).then((res) => {
        this.page.PageCount = res.Data.PageCount;
        this.datalist = res.Data.Data;
        this.listLoading = false;
      });
    },
    //删除
    remove() {
      var rows = this.$refs.mytable.selection;
      if (rows.length == 0) {
        this.$message({
          message: "请选择要删除的数据！",
          type: "error",
        });
        return;
      }
      var arrayIds = [];
      var arrayNames = [];
      var userIds = rows.map((c) => {
        arrayIds.push(c.id);
        arrayNames.push(c.name);
        return c.id;
      });

      this.$confirm("确认删除【" + arrayNames + "】吗?", "提示", {
        type: "warning",
      })
        .then(() => {
          this.listLoading = true;
          let param = qs.stringify({ apiIds: arrayIds });
          removeApi(param).then((res) => {
            if (util.isEmt.format(res)) {
              this.listLoading = false;
              return;
            }
            this.listLoading = false;
            //NProgress.done();
            if (res.data.isSuccess) {
              this.$message({
                message: "删除成功",
                type: "success",
              });
            } else {
              this.$message({
                message: res.data.message,
                type: "error",
              });
            }

            this.search();
          });
        })
        .catch(() => {});
    },
    //显示编辑界面
    update() {
      if (!this.select.hasOwnProperty("ID")) {
        this.$message({
          message: "请选择要编辑的一行数据！",
          type: "error",
        });

        return;
      }
      this.addFormVisible = true;
      this.addForm = Object.assign({}, this.select);
    },
    //显示新增界面
    insert() {
      this.addFormVisible = true;
      this.addForm = Object.assign({}, {});
    },
    //新增
    saveSubmit: function () {
      let _this = this;
      this.$refs.addFormRef.validate((valid) => {
        if (valid) {
          this.addLoading = true;
          let param = Object.assign({}, this.addForm);
          saveApi(param).then((res) => {
            if (util.isEmt.format(res)) {
              this.addLoading = false;
              return;
            }
            if (res.data.IsSuccess) {
              this.addLoading = false;
              //NProgress.done();
              this.$message({
                message: res.data.Message,
                type: "success",
              });
              this.$refs["addFormRef"].resetFields();
              this.addFormVisible = false;
              this.search();
            } else {
              this.$message({
                message: res.data.Message,
                type: "error",
              });
            }
          });
        }
      });
    },
    selsChange: function (row) {
      this.select = row;
      this.$refs.mytable.clearSelection();
      this.$refs.mytable.toggleRowSelection(row);
    },
    //批量删除
    batchRemove: function () {
      this.$message({
        message: "该功能未开放",
        type: "warning",
      });
    },
  },
  mounted() {
    this.search();
    let routers = window.localStorage.router
      ? JSON.parse(window.localStorage.router)
      : [];
    this.buttonList = getButtonList(this.$route.path, routers);
  },
};
</script>

<style scoped>
</style>
